<?php
namespace app\admin\controller;

use app\BaseController;
use app\common\facade\admin\AdminAdmin;
use app\common\facade\admin\RolesAdmin;
use think\exception\ValidateException;

class Roles extends BaseController
{
    /**
     * 角色添加
     */
    public function rolesAdd()
    {
        $param = $this->request->post([
            'id',
            'name',
            'description',
        ]);

        try {            
            $this->validate($param, 'app\common\validate\Roles.addsave');            
            $data = RolesAdmin::roleAddEditSave($param);          
        } catch (ValidateException $e) {
            return validateJson($e->getError());
        } catch (\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson($data);      
    }

    /**
     * 角色栏目列表 tree
     */
    public function rolesList()
    {
        try {            
            $data = AdminAdmin::rolesList();
        } catch (\Exception $e)
        {
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson($data);
    }

    /**
     * 角色列表
     */
    public function rolesLists()
    {
        try {            
            $data = RolesAdmin::rolesLists();
        } catch (\Exception $e)
        {
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson($data);
    }

    /**
     * 角色信息
     */
    public function rolesInfo($id)
    {
        $param = [
            'id' => $id
        ];
        try {
            $data = RolesAdmin::rolesInfo($param);
        } catch (\Exception $e)
        {
            return exceptionJson($e->getMessage(), $e->getCode());
        }
        
        return successJson($data);
    }

    /**
     * 角色删除
     */
    public function rolesDelete($id)
    {
        $param = [
            'id' => $id
        ];

        try {
            $data = RolesAdmin::rolesDelete($param);
        } catch (\Exception $e)
        {
            return exceptionJson($e->getMessage(), $e->getCode());
        }
        
        return successJson($data);
    }

    /**
     * 角色批量删除
     */
    public function rolesDeletes() {
        $param = $this->request->post([
            'idstr'
        ]);

        try {
            $data = RolesAdmin::rolesDeletes($param);
        } catch (\Exception $e)
        {
            return exceptionJson($e->getMessage(), $e->getCode());
        }
        
        return successJson($data);
    }

    /**
     * 角色下权限删除
     */
    public function roleRuleDel($roleid, $ruleid)
    {
        try {
            $data = RolesAdmin::roleRuleDel($roleid, $ruleid);
        } catch (\Exception $e) 
        {
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson($data);
    }

    /**
     * 获取权限列表
     */
    public function rulesAlllist()
    {
        try {
            $data = AdminAdmin::rulesAlllist();
        } catch (\Exception $e) 
        {
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson($data);
    }

    /**
     * 角色权限添加
     */
    public function rolesRuleAdd($roleid)
    {
        $param = $this->request->post([
            'ruleId',
            'id'=>$roleid
        ]);
        
        try {
            $data = RolesAdmin::roleAddEditSave($param);
        } catch (\Exception $e)
        {
            return exceptionJson($e->getMessage(), $e->getCode());
        }
        
        return successJson($data);
    }
}